package com.ant.common.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

/**
 * @author LB
 */
public class DateUtils {

    public static String dateTime2String(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String date2String(Date date) {
        return date2String(date, "yyyy-MM-dd");
    }

    public static String date2String(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static Date string2Date(String string) {
        if (StringUtil.isEmpty(string)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date string2DateTime(String string) {
        if (StringUtil.isEmpty(string)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getNowDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getNowTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date formatDate(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return dateFormat.parse(dateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date timestamp2Date(Long timestamp) {
        Timestamp t = new Timestamp(timestamp);
        return new Date(t.getTime());
    }


    public static Boolean dateEquals(Date date1, Date date2) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date newDate1 = dateFormat.parse(dateFormat.format(date1));
            Date newDate2 = dateFormat.parse(dateFormat.format(date2));
            return newDate1.equals(newDate2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static Long Date2Timestamp(Date date) {
        return date.getTime();
    }

    public static String getNowTime2File() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        return dateFormat.format(new Date());
    }

    public static String getDateStrFromISO8601Timestamp(String ISOdate, String format) {
        if (StringUtil.isEmpty(ISOdate)) {
            return "";
        }
        DateTimeFormatter ISODateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        DateTime dateTime = ISODateTimeFormatter.parseDateTime(ISOdate);
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        return dateTime.toString(dateTimeFormatter);
    }

    public static String getDateStrFromISO8601Timestamp(String ISOdate, String dateFormat, String format) {
        if (StringUtil.isEmpty(ISOdate)) {
            return "";
        }
        DateTimeFormatter ISODateTimeFormatter = DateTimeFormat.forPattern(dateFormat);
        DateTime dateTime = ISODateTimeFormatter.parseDateTime(ISOdate);
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(format);
        return dateTime.toString(dateTimeFormatter);
    }

    public static boolean isDate(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dateFormat.parse(date);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static Date getTimeInMillis(Date date, int minute) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        long time = date.getTime();
        time += minute * 60 * 1000;
        return longToDate(time);
    }

    public static Date longToDate(Long time) {
        int length = time.toString().length();
        if (length == 10) {
            return new Date(time * 1000);
        } else {
            return new Date(time);
        }
    }

    public static Integer getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    public static Integer getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }

    public static Integer getDate() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    public static boolean dateCompare(Date date1, Date date2) {
        if (Objects.isNull(date1)) {
            return false;
        }

        if (dateEquals(date1, date2)) {
            return true;
        }

        return date1.after(date2);
    }

    public static Date getDiffDate(Date date, int diffDays) {
        if (Objects.isNull(date)) {
            return null;
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, diffDays);
        return calendar.getTime();
    }

    public static Date get15DiffDate(Date date) {
        Date diffDate = getDiffDate(date, -15);
        if (!dateCompare(diffDate, new Date())) {
            diffDate = new Date();
        }
        return diffDate;
    }

    /**
     * 获取当前时间，转化为字符串
     * @return
     */
    public static String getNowStr(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }
}
